package com.teacherprofile.manage.controller;

import java.util.List;

import com.teacherprofile.common.domain.SoftwareCopyrights;
import com.teacherprofile.common.utils.StringUtils;
import com.teacherprofile.manage.domain.dto.CopyrightExportDTO;
import com.teacherprofile.manage.domain.dto.PatentsExportDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.teacherprofile.common.annotation.Log;
import com.teacherprofile.common.core.controller.BaseController;
import com.teacherprofile.common.core.domain.AjaxResult;
import com.teacherprofile.common.enums.BusinessType;
import com.teacherprofile.common.domain.Patents;
import com.teacherprofile.manage.service.IPatentsService;
import com.teacherprofile.common.utils.poi.ExcelUtil;
import com.teacherprofile.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;
/**
 * 专利Controller
 *
 * @author qvtu
 * @date 2025-05-07
 */
@Api(tags = "专利")
@RestController
@RequestMapping("/manage/patents")
public class PatentsController extends BaseController {
    @Autowired
    private IPatentsService patentsService;

    /**
     * 查询专利列表
     */
    @ApiOperation(value = "查询专利列表")
    @PreAuthorize("@ss.hasPermi('manage:patents:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam Patents patents) {
        startPage();
        List<Patents> list = patentsService.selectPatentsList(patents);
//        ArrayList<Patents> patentsArrayList = new ArrayList<>();
//        for (Patents patent : list) {
//            Patents patentsResult = new Patents();
//            patentsResult.setId(patent.getId());
//            patentsResult.setNickName(patent.getNickName());
//            patentsResult.setPatentName(patent.getPatentName());
//            patentsResult.setInventors(patent.getInventors());
//            patentsResult.setType(patent.getType());
//            patentsResult.setGrantDate(patent.getGrantDate());
//            patentsResult.setCheckStatus(patent.getCheckStatus());
//            patentsArrayList.add(patentsResult);
//        }
        return getDataTable(list);
    }

    /**
     * 导出专利列表
     */
    @ApiOperation(value = "导出专利列表")
    @PreAuthorize("@ss.hasPermi('manage:patents:export')")
    @Log(title = "专利", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatentsExportDTO patentsExportDTO) {
        List<Patents> list = patentsService.selectPatentsList(patentsExportDTO);
//        System.out.println(patentsExportDTO);
//        System.out.println(list);
        ExcelUtil<Patents> util = new ExcelUtil<Patents>(Patents.class);
        util.exportEasyExcel(response, list, "专利管理数据", StringUtils.str2List(patentsExportDTO.getExportFields(), ",", true, true));

    }

    /**
     * 获取专利详细信息
     */
    @ApiOperation(value = "获取专利详细信息")
    @PreAuthorize("@ss.hasPermi('manage:patents:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(patentsService.selectPatentsById(id));
    }

    /**
     * 新增专利
     */
    @ApiOperation(value = "新增专利")
    @PreAuthorize("@ss.hasPermi('manage:patents:add')")
    @Log(title = "专利", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Patents patents) {
        return toAjax(patentsService.insertPatents(patents));
    }

    /**
     * 修改专利
     */
    @ApiOperation(value = "修改专利")
    @PreAuthorize("@ss.hasPermi('manage:patents:edit')")
    @Log(title = "专利", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Patents patents) {
        return toAjax(patentsService.updatePatents(patents));
    }


    /**
     * 审批
     */
    @ApiOperation(value = "专利审批")
    @PreAuthorize("@ss.hasPermi('manage:copyright:check')")
    @Log(title = "专利管理", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public AjaxResult check(@RequestBody Patents patents) {
        return toAjax(patentsService.check(patents));
    }

    /**
     * 删除专利
     */
    @ApiOperation("删除专利")
    @PreAuthorize("@ss.hasPermi('manage:patents:remove')")
    @Log(title = "专利", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(patentsService.deletePatentsByIds(ids));
    }

    /**
     * 导入专利
     *
     * @param file
     * @return
     * @throws Exception
     */
    @ApiOperation("导入专利")
    @Log(title = "导入专利", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Patents> util = new ExcelUtil<Patents>(Patents.class);
        List<Patents> patentsList = util.importEasyExcel(file.getInputStream());
        String message = patentsService.importData(patentsList);
        System.out.println(message);
        return success(message);
    }

}
